package com.xx.news.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.xx.news.R;
import com.xx.news.activity.ImagerActivity;
import com.xx.news.entity.Jingxuan;

import java.util.List;

public class JingxuanAdapter extends RecyclerView.Adapter<JingxuanAdapter.ViewHolder> {
    private List<Jingxuan.T1467284926140Bean> mList;
    private OnItemClickListener mListener;
    Context context;

    public JingxuanAdapter(List<Jingxuan.T1467284926140Bean> list, Context context) {
        this.context = context;
        mList = list;
    }

    public void updateData(List<Jingxuan.T1467284926140Bean> list) {
        mList.clear();
        mList = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        Jingxuan.T1467284926140Bean resultDTO = mList.get(position);
        holder.textView1.setText(resultDTO.getTitle());
        holder.tv_ly.setText(resultDTO.getDigest());
        holder.tv_source.setText(resultDTO.getSource());
        Glide.with(holder.image.getContext())
                .asBitmap()
                .load(resultDTO.getImgsrc().replace(",", ":"))
                .into(holder.image);
        holder.textView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onItemClick(resultDTO);
                }
            }
        });
        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ImagerActivity.class);
                intent.putExtra("url", resultDTO.getImgsrc().replace(",", ":"));
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView1;
        TextView tv_ly;
        TextView tv_source;
        ImageView image;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.image);
            textView1 = itemView.findViewById(R.id.name);
            tv_ly = itemView.findViewById(R.id.tv_ly);
            tv_source = itemView.findViewById(R.id.tv_source);
        }
    }

    public void addItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(Jingxuan.T1467284926140Bean item);
    }
}
